/*
 * @Author: tcosfish
 * @Date: 2022-07-27 15:03:23
 * @LastEditors: tcosfish
 * @LastEditTime: 2022-08-18 11:35:27
 * @FilePath: \loadFile\src\excel\feat\create_month_report.temp.ts
 */

import ExcelJS from "exceljs"
import { tableTemplate } from "../utils/tableTemplate.util"
import ry_excel from "@/excel/RyExcel"
import ry_database from "@/database/RyDataBase"
import { SubjectBalanceView } from "@/database/views/SubjectBalance.view"
import { CostDetailsView } from "@/database/views/CostDetails.view"

import { join } from "path"

function templatePath(name: string) {
  return join(
    __dirname,
    "../../../",
    `public/uploads/xlsx/template/${name}.xlsx`
  )
}

// 把函数编写当成思维导图的建立对待
// 下一步便是通过配置而不是一大串
async function createMonthlyReport(saveFilePath: string, reportOptions?: any) {
  // 设置查询字段
  const showCols1 = ["view.name", "view.debit", "view.credit"]
  const showCols2 = ["view.name", "view.debit_amount"]

  // 从数据库中查询数据, 并进行格式化
  const connected = await ry_database.init()
  const databaseResult1 = await connected.getData(
    "1002_%",
    showCols1,
    SubjectBalanceView
  )
  const databaseResult2 = await connected.getData(
    "1122_%",
    showCols1,
    SubjectBalanceView
  )
  const databaseResult3 = await connected.getData(
    "1123_%",
    showCols1,
    SubjectBalanceView
  )
  const databaseResult4 = await connected.getData(
    "1221_%",
    showCols1,
    SubjectBalanceView
  )
  const databaseResult5 = await connected.getData(
    "2202_%",
    showCols1,
    SubjectBalanceView
  )
  const databaseResult6 = await connected.getData(
    "2241_%",
    showCols1,
    SubjectBalanceView
  )
  const databaseResult7 = await connected.getData(
    "2501_%",
    showCols1,
    SubjectBalanceView
  )
  const databaseResultX1 = await connected.getData(
    "6601_%",
    showCols2,
    CostDetailsView
  )
  const databaseResultX2 = await connected.getData(
    "6602_%",
    showCols2,
    CostDetailsView
  )
  const tableData1 = databaseResult1.map((row) => Object.values(row))
  const tableData2 = databaseResult2.map((row) => Object.values(row))
  const tableData3 = databaseResult3.map((row) => Object.values(row))
  const tableData4 = databaseResult4.map((row) => Object.values(row))
  const tableData5 = databaseResult5.map((row) => Object.values(row))
  const tableData6 = databaseResult6.map((row) => Object.values(row))
  const tableData7 = databaseResult7.map((row) => Object.values(row))
  const tableDataX1 = databaseResultX1.map((row) => Object.values(row))
  const tableDataX2 = databaseResultX2.map((row) => Object.values(row))

  // 段落计数君应该在内部就要实现
  // 将数据渲染到excel文件中
  await ry_excel
    .createTemplate()
    .addSheet("1.资产负债表")
    .copy(templatePath("资产负债表"), "Sheet1", 1)
  await ry_excel
    .addSheet("2.损益表")
    .copy(templatePath("损益表模板"), "Sheet1", 2)
  ry_excel
    .addSheet("3.科目余额表", { table_spacing: 3, showTotal: true })
    .addTable("库存现金表", `A1`, createCols("库存现金表"), tableData1)
    .addTable("应收账款表", `A12`, createCols("应收账款表"), tableData2)
    .addTable("预付账款表", `A37`, createCols("预付账款表"), tableData3)
    .addTable("其他应收账款表", `A47`, createCols("其他应收账款表"), tableData4)
    .addTable("应付账款表", `A73`, createCols("应付账款表"), tableData5)
    .addTable("其他应付款表", `A85`, createCols("其他应付款表"), tableData6)
    .addTable("长期借款表", `A99`, createCols("长期借款表"), tableData7)
  await ry_excel
    .addSheet("4.预算表")
    .copy(templatePath("预算表模板"), "Sheet1", 4)
  ry_excel
    .addSheet("5.成本明细表", { table_spacing: 3, showTotal: true })
    .addTable(
      "销售费用明细表",
      `A1`,
      createCols2("销售费用明细表"),
      tableDataX1
    )
    .addTable(
      "管理费用明细表",
      `A12`,
      createCols2("管理费用明细表"),
      tableDataX2
    )
  await ry_excel
    .addSheet("6.资金动态表")
    .copy(templatePath("资金动态表模板"), "Sheet1", 6)
  await ry_excel.saveFile(saveFilePath)
}

function createCols(tableName: string) {
  return [
    { name: "名称", totalsRowFunction: "none", totalsRowLabel: "总计" },
    { name: "期末借方余额", totalsRowFunction: "sum" },
    { name: "期末贷方余额", totalsRowFunction: "sum" },
    { name: tableName },
  ]
}

function createCols2(tableName: string) {
  return [
    { name: "名称", totalsRowFunction: "none", totalsRowLabel: "总计" },
    { name: "本期借方发生额", totalsRowFunction: "sum" },
    { name: tableName },
  ]
}

export { createMonthlyReport, templatePath }
